package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 菜单结构DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "年级菜单结构")
public class MenuDTO {

    @NotNull(message = "年级ID不能为空")
    @Positive(message = "年级ID必须是正整数")
    @Schema(description = "年级唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer gradeId;

    @NotBlank(message = "年级名称不能为空")
    @Schema(description = "年级名称", example = "一年级", requiredMode = Schema.RequiredMode.REQUIRED)
    private String gradeName;

    @Schema(description = "年级描述", example = "小学一年级")
    private String gradeDescription;

    @Schema(description = "排序顺序", example = "1")
    private Integer sortOrder;

    @Schema(description = "分类列表")
    private List<CategoryDTO> categories;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "菜单分类信息")
    public static class CategoryDTO {

        @NotNull(message = "分类ID不能为空")
        @Positive(message = "分类ID必须是正整数")
        @Schema(description = "分类唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "分类名称不能为空")
        @Schema(description = "分类名称", example = "语文", requiredMode = Schema.RequiredMode.REQUIRED)
        private String name;

        @Schema(description = "分类图标", example = "chinese-icon.png")
        private String icon;

        @Schema(description = "分类描述", example = "语文学习资源")
        private String description;

        @Schema(description = "排序顺序", example = "1")
        private Integer sortOrder;

        @Schema(description = "是否启用", example = "true")
        private Boolean enabled;

        @Schema(description = "子分类列表")
        private List<SubcategoryDTO> subcategories;

        /**
         * 获取子分类数量
         */
        @Schema(description = "子分类数量", accessMode = Schema.AccessMode.READ_ONLY)
        public Integer getSubcategoryCount() {
            return subcategories != null ? subcategories.size() : 0;
        }

        /**
         * 验证分类是否有效
         */
        public boolean isValid() {
            return id != null && name != null && !name.trim().isEmpty();
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "子分类信息")
    public static class SubcategoryDTO {

        @NotNull(message = "子分类ID不能为空")
        @Positive(message = "子分类ID必须是正整数")
        @Schema(description = "子分类唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "子分类名称不能为空")
        @Schema(description = "子分类名称", example = "古诗词", requiredMode = Schema.RequiredMode.REQUIRED)
        private String name;

        @Schema(description = "子分类描述", example = "经典古诗词学习")
        private String description;

        @Schema(description = "排序顺序", example = "1")
        private Integer sortOrder;

        @Schema(description = "资源数量", example = "50")
        private Integer resourceCount;

        @Schema(description = "是否启用", example = "true")
        private Boolean enabled;

        /**
         * 获取完整路径名称
         */
        @Schema(description = "完整路径名称", accessMode = Schema.AccessMode.READ_ONLY)
        public String getFullPathName(CategoryDTO parentCategory, MenuDTO parentMenu) {
            StringBuilder path = new StringBuilder();
            if (parentMenu != null) {
                path.append(parentMenu.getGradeName()).append(" - ");
            }
            if (parentCategory != null) {
                path.append(parentCategory.getName()).append(" - ");
            }
            path.append(name);
            return path.toString();
        }
    }

    /**
     * 获取分类数量
     */
    @Schema(description = "分类数量", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getCategoryCount() {
        return categories != null ? categories.size() : 0;
    }

    /**
     * 获取总子分类数量
     */
    @Schema(description = "总子分类数量", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getTotalSubcategoryCount() {
        if (categories == null) return 0;
        return categories.stream()
                .mapToInt(category -> category.getSubcategoryCount())
                .sum();
    }

    /**
     * 获取启用的分类列表
     */
    @Schema(description = "启用的分类列表", accessMode = Schema.AccessMode.READ_ONLY)
    public List<CategoryDTO> getEnabledCategories() {
        if (categories == null) return List.of();
        return categories.stream()
                .filter(category -> category.getEnabled() == null || category.getEnabled())
                .toList();
    }
}
